package com.smartResponse;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

	public class MessageLists extends ListActivity {
		@Override
		public void onCreate(Bundle icicle) {
			super.onCreate(icicle);
			EventDB db = new EventDB(this);
			ArrayAdapter<String> messages = new ArrayAdapter<String>(this,
	                android.R.layout.simple_list_item_1);
			//Add the Add button
			messages.add("Add a new message");
		    db.open();
	        Cursor cursor = db.getAllMessages();
	        while (cursor.moveToNext())
	        {
	            String results = cursor.getString(1);
	            messages.add(results);
	        }
	        db.close();
			setListAdapter(messages);
		}

		@Override
		protected void onListItemClick(ListView l, View v, int position, long id) {
			String current_message = (String) getListAdapter().getItem(position);
			//Log.d("Test",current_message);
			if (current_message.equals("Add a new message")){
   			  Intent add_intent_menu = new Intent(MessageLists.this, AddEvent.class);
   			 startActivityForResult(add_intent_menu,1);
   			 finish();
			}
			else{
				ViewMessage.change_name(current_message,false); 
			    Intent i = new Intent(MessageLists.this, ViewMessage.class);     
			    i.putExtra("message",current_message);
		        startActivityForResult(i, 1);
			}

		}
		//The code in the function below checks to see if a new message was added and if so it refreshes the page
	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);
	        
	        if(resultCode==1){
	        	SmartResponseActivity.setCurrentTab(1);
	            Intent refresh = new Intent(MessageLists.this, SmartResponseActivity.class);
	            startActivity(refresh);
	            this.finish();
	        }
	        if(resultCode==RESULT_CANCELED){
	        	SmartResponseActivity.setCurrentTab(1);
	            Intent refresh = new Intent(MessageLists.this, SmartResponseActivity.class);
	            startActivity(refresh);
	            this.finish();
	        }
	        
	    }
		// Menu Layout
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			super.onCreateOptionsMenu(menu);
	        menu.add(0,1,0,"Home").setIcon(R.drawable.home);
	        menu.add(0,2,0,"Add").setIcon(R.drawable.add_icon);
		    return true;
		}
	    @Override
	     public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        case 1:
				  Intent home_intent_menu = new Intent(MessageLists.this, MainActivity.class);
				  startActivity(home_intent_menu);
   			  
   			  return true;
	        case 2:
     			  Intent add_intent_menu = new Intent(MessageLists.this, AddEvent.class);
     			 startActivityForResult(add_intent_menu,1);

	            return true;
	          }
			return false;
	    }
}
